﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI.Controls
{
    public partial class RoomViewControl : UserControl
    {
        public string RoomType = "";
        public string RoomStatus = "";
        public delegate void ShowStatusHandler(RoomInfo roomInfo);
        public ShowStatusHandler OnShowStatus = null;

        public RoomViewControl()
        {
            InitializeComponent();
        }

        private int GetRoomImage(Entity.RoomStatus roomStatus)
        {
            int result = 0;
            switch (roomStatus)
            {
                case Entity.RoomStatus.空净房:
                    result = 0;
                    break;
                case Entity.RoomStatus.空脏房:
                    result = 1;
                    break;
                case Entity.RoomStatus.住净房:
                    result = 2;
                    break;
                case Entity.RoomStatus.住脏房:
                    result = 3;
                    break;
                case Entity.RoomStatus.维修房:
                    result = 4;
                    break;
                case Entity.RoomStatus.预定房:
                    result = 5;
                    break;
                case Entity.RoomStatus.锁房:
                    result =6;
                    break;
                case Entity.RoomStatus.强制锁房:
                    result =7;
                    break;
            }

            return result;
        }

        private void BindData()
        {
            this.listView1.Items.Clear();
            this.listView1.Columns.Clear();

            #region 创建视图显示列
            listView1.Columns.Add("房间号", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("状态", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("消费金额", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("进店时间", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("预离日期", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("已用时间", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("房间电话", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("所属区域", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("开房次数", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("备注", 100, HorizontalAlignment.Left);
            #endregion

            if (!string.IsNullOrEmpty(RoomType))
            {
                List<RoomInfo> roomList = BLLFactory<WHC.Hotel.BLL.Room>.Instance.FindRooms(RoomType, RoomStatus);
                foreach (RoomInfo info in roomList)
                {
                    #region MyRegion
                    ListViewItem item1 = new ListViewItem(info.RoomNo, GetRoomImage(info.Status));
                    item1.SubItems.Add(info.Status.ToString());//状态
                    item1.SubItems.Add("2");//消费金额
                    item1.SubItems.Add("");//进店时间
                    item1.SubItems.Add("");//预离日期
                    item1.SubItems.Add("");//已用时间
                    item1.SubItems.Add(info.RoomTelephone);//房间电话
                    item1.SubItems.Add(info.BuildNum);//所属区域
                    item1.SubItems.Add("");//开房次数
                    item1.SubItems.Add("");//备注
                    item1.Tag = info;
                    item1.ToolTipText = string.Format("{0}:{1}", info.RoomNo, info.Status);

                    this.listView1.Items.Add(item1); 
                    #endregion
                }
            }
        }

        private void RoomViewControl_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            ListViewItem item = listView1.GetItemAt(e.X, e.Y);
            if (OnShowStatus != null)
            {
                RoomInfo info = null;
                if (item != null)
                {
                    info = item.Tag as RoomInfo;
                }

                OnShowStatus(info);
            }
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
                return;

            ListViewItem item = listView1.GetItemAt(e.X, e.Y);
            if (item != null)
            {
                RoomInfo info = item.Tag as RoomInfo;
                if (info == null) return;

                #region 根据不同的房态处理不同的事件

                if (info.Status == Entity.RoomStatus.维修房  )
                {
                    ChangeStatus();
                }
                else if (info.Status == Entity.RoomStatus.空净房 ||
                   info.Status == Entity.RoomStatus.预定房)
                {
                    menu_SankeBill_Click(null, null);
                }
                else if (info.Status == Entity.RoomStatus.住净房 || info.Status == Entity.RoomStatus.住脏房 
                   )
                {
                    menu_CheckOut_Click(null, null);//结账
                }

                #endregion
            }
        }

        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button.ToString() == "Right")
            {
                ListViewItem item = listView1.GetItemAt(e.X, e.Y);
                if (item != null)
                {
                    RoomInfo info = item.Tag as RoomInfo;
                    if (info == null) return;

                    #region 根据不同的房态分配菜单
                    //if (info.Status == Entity.RoomStatus.空净房 ||
                    //    info.Status == Entity.RoomStatus.预定)
                    //{
                    //    EnableAllContextMenu(false);
                    //    this.listView1.ContextMenuStrip = this.contextMenuStrip1;
                    //    this.menu_ChangeStatus.Enabled = true;

                    //    this.menu_SankeBill.Enabled = true;
                    //    this.menu_GroupBill.Enabled = true;
                    //    this.menu_BookIn.Enabled = true;
                    //    this.menu_Baofang.Enabled = true;
                    //}
                    //else if (info.Status == Entity.RoomStatus.维修房 ||
                    //         info.Status == Entity.RoomStatus.清理)
                    //{
                    //    EnableAllContextMenu(false);
                    //    this.listView1.ContextMenuStrip = this.contextMenuStrip1;
                    //    this.menu_ChangeStatus.Enabled = true;

                    //    this.menu_GroupBill.Enabled = true;
                    //    this.menu_BookIn.Enabled = true;
                    //}
                    //else if (info.Status == Entity.RoomStatus.占用)
                    //{
                    //    EnableAllContextMenu(true);
                    //    this.listView1.ContextMenuStrip = this.contextMenuStrip1;
                    //    this.menu_ChangeStatus.Enabled = true;

                    //    this.menu_SankeBill.Enabled = false;
                    //    this.menu_Baofang.Enabled = false;
                    //    this.menu_BookIn.Enabled = false;
                    //}
                    //else if (info.Status == Entity.RoomStatus.长包房)
                    //{
                    //    EnableAllContextMenu(false);
                    //    this.listView1.ContextMenuStrip = this.contextMenuStrip1;
                    //    this.menu_ChangeStatus.Enabled = true;

                    //    this.menu_AddConsume.Enabled = true;
                    //    this.menu_CheckOut.Enabled = true;
                    //    this.menu_CheckPart.Enabled = true;
                    //    this.menu_GroupBill.Enabled = true;
                    //    this.menu_BookIn.Enabled = true;
                    //}

                    #endregion
                }
                else
                {
                    this.listView1.ContextMenuStrip = null;
                }
            }
        }

        private void EnableAllContextMenu(bool enable)
        {
            foreach (ToolStripItem menu in this.contextMenuStrip1.Items)
            {
                menu.Enabled = enable;
            }
        }

        private void ChangeStatus()
        {
            RoomInfo info = GetSelectRoomInfo();
            if (info != null)
            {
                FrmChangeRoomStatus dlg = new FrmChangeRoomStatus();
                dlg.RoomInfo = info;
                dlg.ShowDialog();
                BindData();
            }            
        }

        private RoomInfo GetSelectRoomInfo()
        {
            RoomInfo info = null;
            if (this.listView1.SelectedItems.Count > 0)
            {
                ListViewItem item = this.listView1.SelectedItems[0];
                if (item != null)
                {
                    info = item.Tag as RoomInfo;
                }
            }
            return info;
        }

        private void menu_ChangeStatus_Click(object sender, EventArgs e)
        {
            ChangeStatus();
        }

        /// <summary>
        /// 修改ListView的视图
        /// </summary>
        /// <param name="viewType"></param>
        public void ChangeViewType(View viewType)
        {
            this.listView1.View = viewType;
            BindData();
            this.Refresh();
        }

        /// <summary>
        /// 修改房间的状态显示
        /// </summary>
        /// <param name="roomStatus"></param>
        public void ChangeRoomStatus(string roomStatus)
        {
            this.RoomStatus = roomStatus;
            BindData();
            this.Refresh();
        }

        public void UpdateStatus()
        {
            BindData();
            this.Refresh();
        }

        #region 菜单操作

        private void menu_SankeBill_Click(object sender, EventArgs e)
        {
            if (!Portal.gc.bRegisted)
            {
                int orderCount = BLLFactory<Order>.Instance.GetRecordCount();
                if (orderCount > 50)
                {
                    MessageUtil.ShowWarning("您好，未注册版本开单数量有限制，请注册后再继续使用");
                    return;
                }
            }

            RoomInfo info = GetSelectRoomInfo();
            if (info != null)
            {
                if (info.Status == Entity.RoomStatus.空净房)
                {
                    FrmEditRoomOrder dlg = new FrmEditRoomOrder();
                    dlg.RoomInfo = info;
                    dlg.ShowDialog();
                    BindData();
                }
                else if (info.Status == Entity.RoomStatus.预定房)
                {
                    FrmBookCondition dlg = new FrmBookCondition();
                    dlg.RoomInfo = info;
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        FrmEditRoomOrder dlgOrder = new FrmEditRoomOrder();
                        dlgOrder.RoomInfo = info;
                        if (dlg.bookInfo == null)
                        {
                            dlgOrder.ShowDialog();
                        }
                        else
                        {
                            BookInfo bookInfo = dlg.bookInfo;
                            dlgOrder.txtCompanyName.Text = bookInfo.CustomerCompany;
                            dlgOrder.txtName.Text = bookInfo.CustomerName;
                            dlgOrder.txtSource.Text = bookInfo.Source;
                            dlgOrder.txtTelephone.Text = bookInfo.CustomerTel;
                            if (dlgOrder.ShowDialog() == DialogResult.OK)
                            {
                                //删除预订
                                BLLFactory<Book>.Instance.Delete(bookInfo.ID.ToString());
                            }
                        }
                    }
                    BindData();
                }
            }
        }

        private void menu_CheckPart_Click(object sender, EventArgs e)
        {
            RoomInfo info = GetSelectRoomInfo();
            if (info != null)
            {
                FrmPartCheckOut dlg = new FrmPartCheckOut();
                dlg.RoomInfo = info;
                dlg.ShowDialog();
                BindData();
            }
        }

        private void menu_AddConsume_Click(object sender, EventArgs e)
        {
            RoomInfo info = GetSelectRoomInfo();
            if (info != null)
            {
                FrmAddConsume dlg = new FrmAddConsume();
                dlg.RoomInfo = info;
                dlg.ShowDialog();
                BindData();
            }
        }

        private void menu_CheckOut_Click(object sender, EventArgs e)
        {
            RoomInfo info = GetSelectRoomInfo();
            if (info != null)
            {
                if (info.Status == Entity.RoomStatus.住净房)
                {
                    FrmRoomCheckOut dlg = new FrmRoomCheckOut();
                    dlg.RoomInfo = info;
                    dlg.ShowDialog();
                }
                //else if (info.Status == Entity.RoomStatus.长包房)
                //{
                //    //一样的处理方式
                //    FrmRoomCheckOut dlg = new FrmRoomCheckOut();
                //    dlg.RoomInfo = info;
                //    dlg.ShowDialog();
                //}
                //BindData();
            }     
        }

        private void menu_GroupBill_Click(object sender, EventArgs e)
        {
            if (!Portal.gc.bRegisted)
            {
                int orderCount = BLLFactory<Order>.Instance.GetRecordCount();
                if (orderCount > 50)
                {
                    MessageUtil.ShowWarning("您好，未注册版本开单数量有限制，请注册后再继续使用");
                    return;
                }
            }

            RoomInfo info = GetSelectRoomInfo();
            if (info != null)
            {
                FrmEditRoomGroupOrder dlg = new FrmEditRoomGroupOrder();
                dlg.RoomInfo = info;
                dlg.ShowDialog();
                BindData();
            }     
        }

        private void menu_ModifyDeposit_Click(object sender, EventArgs e)
        {
            RoomInfo info = GetSelectRoomInfo();
            if (info != null)
            {
                FrmEditDeposit dlg = new FrmEditDeposit();
                dlg.RoomInfo = info;
                dlg.ShowDialog();
                BindData();
            }
        }

        private void menu_ModifyRegister_Click(object sender, EventArgs e)
        {
            RoomInfo info = GetSelectRoomInfo();
            if (info != null)
            {
                FrmEditRoomRegister dlg = new FrmEditRoomRegister();
                dlg.RoomInfo = info;
                dlg.ShowDialog();
                BindData();
            }
        }

        private void menu_SwithRoom_Click(object sender, EventArgs e)
        {
            RoomInfo info = GetSelectRoomInfo();
            if (info != null)
            {
                FrmSwitchRoom dlg = new FrmSwitchRoom();
                dlg.RoomInfo = info;
                dlg.ShowDialog();
                BindData();
            }
        }

        private void menu_MergeBill_Click(object sender, EventArgs e)
        {

        }

        private void menu_SplitBill_Click(object sender, EventArgs e)
        {

        }

        private void menu_BookIn_Click(object sender, EventArgs e)
        {
            RoomInfo info = GetSelectRoomInfo();
            if (info != null)
            {
                FrmBookRoom dlg = new FrmBookRoom();
                dlg.RoomInfo = info;
                dlg.ShowDialog();
                BindData();
            }
        }

        private void menu_Baofang_Click(object sender, EventArgs e)
        {
            RoomInfo info = GetSelectRoomInfo();
            if (info != null)
            {
                FrmContractOrder dlg = new FrmContractOrder();
                dlg.RoomInfo = info;
                dlg.ShowDialog();
                BindData();
            }
        } 
        #endregion
    }
}
